package com.lancoo.znbkxx.teacher.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View
import com.lancoo.znbkxx.teacher.R

/**
 * Created by wealive on 2021/3/24.
 *
 *
 */
class TriangleView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null,
                                             defStyleAttr: Int = 0) :
        View(context, attrs, defStyleAttr) {
    var paint = Paint()
    val path = Path()
    private var mColor: Int = 0

    init {
        setViewAttributes(context, attrs, defStyleAttr)
    }

    private fun setViewAttributes(context: Context, attrs: AttributeSet?, defStyleAttr: Int) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.TriangleView,
                defStyleAttr, 0)
        mColor = typedArray.getColor(R.styleable.TriangleView_mColor, Color.parseColor("#00afff"))
        typedArray.recycle()
    }

    override fun onDraw(canvas: Canvas?) {
        paint.color = mColor
        paint.isAntiAlias = true
        path.moveTo(4f, 0f)
        path.lineTo(0f, 4f)
        path.lineTo(8f, 4f)
        path.lineTo(4f, 0f)
        canvas?.drawPath(path, paint)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(8, 4)
    }
}